package com.system.utils;

import com.system.po.Plan;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 组装plan树解构
 */
public class MakePlanTree {
    public static List<Plan> makeTree(List<Plan> planList, Integer praentId){
        List<Plan> list = new ArrayList<>();
        Optional.ofNullable(planList).orElse(new ArrayList<>())
                .stream()
                .filter(Plan -> Plan.getParentId() == praentId)
                .forEach(Plan -> {
                    Plan plan = new Plan();
                    BeanUtils.copyProperties(Plan, plan);
                    List<Plan> children = makeTree(planList,plan.getId());
                    plan.setChildren(children);
                    list.add(plan);
                });
        return list;
    }

}
